package integraltask

import (
	"context"
	"strings"
	"time"

	"mall/common/modelx"
	"mall/service/backend/common/errorx"
	"mall/service/backend/common/i18n"
	"mall/service/setting/model"
	"mall/service/setting/rpc/internal/svc"
	"mall/service/setting/rpc/internal/utils/errorhandler"
	"mall/service/setting/rpc/types/setting"

	"github.com/zeromicro/go-zero/core/logx"
)

type BackendCreateInteGralTaskLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewBackendCreateIntegralTaskLogic(ctx context.Context, svcCtx *svc.ServiceContext) *BackendCreateInteGralTaskLogic {
	return &BackendCreateInteGralTaskLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

// # 2025.02.15 IntegralTask #
func (l *BackendCreateInteGralTaskLogic) BackendCreateIntegralTask(in *setting.IntegralTaskInfo) (*setting.BaseIDResp, error) {
	logx.Infof("BackendCreateIntegralTask in : %+v", in)
	var task model.SysUserIntegralTask
	var name, slug string
	if in.RuleName == nil || len(strings.TrimSpace(*in.RuleName)) == 0 {
		logx.Errorw("create member task failed, its name is required")
		return nil, errorx.NewInvalidArgumentError("setting.memberTaskNameRequired")
	}
	name = strings.TrimSpace(*in.RuleName)
	if in.RuleSlug != nil && len(strings.TrimSpace(*in.RuleSlug)) > 0 {
		slug = strings.TrimSpace(*in.RuleSlug)
	}

	filterMap := make(map[string]map[string][]any)
	filterMap[model.SysUserIntegralTaskColumns.RuleName] = map[string][]any{
		"equalTo": {name},
	}

	if len(strings.TrimSpace(slug)) > 0 {
		filterMap[model.SysUserIntegralTaskColumns.RuleSlug] = map[string][]any{
			"equalTo": {slug},
		}
	}

	_, count, err := l.svcCtx.IntegralTask.GetList(l.ctx, model.M{}, 0, filterMap)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}

	if count > 0 {
		return nil, errorx.NewAlreadyExistsError("setting.memberTaskAlreadyExists")
	}
	task.RuleName = name
	task.RuleSlug = slug
	if in.RuleType != nil && *in.RuleType > 0 {
		task.RuleType = uint16(*in.RuleType)
	}
	if in.PicId != nil && *in.PicId > 0 {
		task.PicID = *in.PicId
	}
	if in.PicUrl != nil && len(strings.TrimSpace(*in.PicUrl)) > 0 {
		task.PicURL = *in.PicUrl
	}
	if in.Frequency != nil && *in.Frequency > 0 {
		task.Frequency = uint16(*in.Frequency)
	}
	if in.Points != nil && *in.Points > 0 {
		task.Points = *in.Points
	}
	if in.EffectiveTime != nil && len(strings.TrimSpace(*in.EffectiveTime)) > 0 {
		task.EffectiveTime, err = modelx.ParseLocalDate(*in.EffectiveTime)
		if err != nil {
			return nil, err
		}
	}
	if in.Ip != nil && len(strings.TrimSpace(*in.Ip)) > 0 {
		task.IP = *in.Ip
	}
	if in.Sort != nil && *in.Sort == 0 {
		task.Sort = uint16(*in.Sort)
	} else {
		task.Sort = uint16(9999)
	}
	if in.Description != nil && len(strings.TrimSpace(*in.Description)) > 0 {
		task.Description = modelx.GetNullString(*in.Description)
	}

	if in.Status != nil {
		task.Status = uint8(*in.Status)
	}
	task.CreatedAt = time.Now()
	task.UpdatedAt = time.Now()
	err = l.svcCtx.IntegralTask.Insert(l.ctx, nil, &task)
	if err != nil {
		return nil, errorhandler.DefaultRepositoryError(l.Logger, err, in)
	}
	return &setting.BaseIDResp{
		Id:  task.ID,
		Msg: i18n.CreateSuccess,
	}, nil
}
